package com.lanxiao.cloud.modules.dept.domain.service.impl;

import com.lanxiao.cloud.common.core.exception.BusinessException;
import com.lanxiao.cloud.common.mybatis.service.impl.BaseServiceImpl;
import com.lanxiao.cloud.modules.dept.domain.model.Dept;
import com.lanxiao.cloud.modules.dept.domain.service.IDeptService;
import com.lanxiao.cloud.modules.dept.infrastructure.error.DeptErrorType;
import com.lanxiao.cloud.modules.dept.infrastructure.repository.DeptMapper;
import java.util.Objects;
import org.springframework.stereotype.Service;

/**
 * @author ：qiaoliang
 * @date ：2021-10-26
 */
@Service
public class DeptServiceImpl extends
    BaseServiceImpl<Long, DeptMapper, Dept> implements
    IDeptService {

  @Override
  public void saveDept(Dept dept) {
    final Dept parentDept = this.getById(dept.getParentId());
    BusinessException.isNull(parentDept, DeptErrorType.PARENT_DEPT_NULL_ERROR);
    dept.fillParentIds(parentDept);
    this.save(dept);
  }

  @Override
  public void updateDept(Dept dept) {
    if(Objects.nonNull(dept.getParentId())) {
      final Dept parentDept = this.getById(dept.getParentId());
      BusinessException.isNull(parentDept, DeptErrorType.PARENT_DEPT_NULL_ERROR);
      dept.fillParentIds(parentDept);
    }
    this.updateById(dept);
  }

}
